package com.pdg.dungeondelver.engine;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.pdg.dungeondelver.configuration.Configuration;
import com.pdg.dungeondelver.configuration.Descriptor;

public class Layer extends DescriptoredHashMap<Integer,LayerColumn> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7658278238723250593L;
	private AtlasCell atlasCell;
	private Map<AtlasDirections,Layer> neighbors = new HashMap<AtlasDirections,Layer>();
	public void setNeighbor(AtlasDirections theDirection, Layer theCell){
		neighbors.put(theDirection,theCell);
	}
	public Layer getNeighbor(AtlasDirections theDirection){
		return neighbors.get(theDirection);
	}
	public Iterator<AtlasDirections> getNeighborDirectionsIterator(){
		return neighbors.keySet().iterator();
	}

	public Layer(AtlasCell theAtlasCell){
		super(new Descriptor());
		setCell(theAtlasCell);
	}

	private void setCell(AtlasCell theAtlasCell) {
		atlasCell = theAtlasCell;
		
	}
	public AtlasCell getCell(){
		return atlasCell;
	}
	public Configuration getConfiguration(){
		return getCell().getConfiguration();
	}
	public Atlas getAtlas(){
		return getCell().getAtlas();
	}
	public Integer getLayerId(){
		Iterator<Map.Entry<Integer,Layer>> iterator = getCell().entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<Integer,Layer> entry = iterator.next();
			if(entry.getValue().equals(this)){
				return entry.getKey();
			}
		}
		return null;
	}
	private Set<AtlasDirections> connections = new HashSet<AtlasDirections>();
	public void setConnection(AtlasDirections direction, Boolean b) {
		if(b){
			if(!connections.contains(direction)){
				connections.add(direction);
			}
		}else{
			if(connections.contains(direction)){
				connections.remove(direction);
			}
		}
	}
	public Boolean getConnection(AtlasDirections direction){
		return connections.contains(direction);
	}
	public Integer getConnectionCount(){
		return connections.size();
	}
	public void clearConnections(){
		connections.clear();
	}
}
